package com.ghxxzx.daglcustom.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ghxxzx.daglcustom.entity.User;
import com.ghxxzx.daglcustom.mapper.UserMapper;
import com.ghxxzx.daglcustom.vo.ResultEntity;
import com.ghxxzx.daglcustom.mapper.UserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * desc:
 *
 * @author cx316
 * Created by cx316 on 2020-05-29.
 */
@RestController
public class LoginController {
    Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    UserMapper userMapper;
    @RequestMapping("doLogin")
    public ResultEntity login(User user, HttpSession session){
        User user2 = userMapper.selectByUserNameAndPassword(user.getAccount(),user.getPassword());
        if(user2 != null){
            session.setAttribute("user",user2);
            session.setMaxInactiveInterval(30*24*60*60);
            return ResultEntity.ok();
        }
        return ResultEntity.error("账户或密码错误");
    }
    @RequestMapping("zxLogin")
    public ResultEntity zxLogin(HttpSession session) {
        session.invalidate();
        return ResultEntity.ok();
    }
    @RequestMapping("who")
    public ResultEntity who(HttpSession session){
        Object user = session.getAttribute("user");
        return ResultEntity.ok(user);
    }
}
